package Bill.app.client;

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.Socket;
import java.net.UnknownHostException;

import javax.swing.*;

import Bill.util.Configuration;

class GroupChat extends JFrame
    implements KeyListener, WindowListener, Runnable
{

    GroupChat(String login, String Etat)
    {
        super("BZHMessenger 1.0.0 Discussion de group!");
        message = new JList();
        text = new JTextField();
        socket = null;
        Configuration.loadConfiguration();
        port = Configuration.Config_portServeurGroup;//portServeurGroup;
        lecture = null;
        ecriture = null;
        Ipserveur = Configuration.IPserveur;
        this.login = "";
        this.Etat = null;
        JScrollPane scroller = new JScrollPane(message);
        scroller.setVerticalScrollBarPolicy(22);
        scroller.setHorizontalScrollBarPolicy(30);
        scroller.setPreferredSize(new Dimension(300, 150));
        scroller.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createCompoundBorder(BorderFactory.createTitledBorder(""), BorderFactory.createEmptyBorder(1, 1, 1, 1)), scroller.getBorder()));
        add("Center", scroller);
        message.setVisible(true);
        message.setCellRenderer(new LabelListCellRendererM());
        message.setFont(new Font("Serif", 1, 20));
        add("South", text);
        text.addKeyListener(this);
        addWindowListener(this);
        setSize(500, 600);
        Dimension dimensionecran = Toolkit.getDefaultToolkit().getScreenSize();
        setLocation((dimensionecran.width - getWidth()) / 2, (dimensionecran.height - getHeight()) / 2);
        setResizable(false);
        setIconImage((new ImageIcon("images.jpg")).getImage());
        this.login = login;
        this.Etat = Etat;
        try
        {
            socket = new Socket(Ipserveur, port);
        }
        catch(UnknownHostException e1)
        {
            e1.printStackTrace();
        }
        catch(IOException e1)
        {
            e1.printStackTrace();
        }
        try
        {
            ecriture = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true);
            ecriture.println(login);
            lecture = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            new GroupChatRecevoir(lecture, message, socket, this.Etat);
        }
        catch(IOException e)
        {
            e.printStackTrace();
        }
        setVisible(true);
    }

    public void run()
    {
        if(Etat.compareToIgnoreCase("CLOSED") == 0)
            (new Thread()).stop();
    }

    public void windowActivated(WindowEvent windowevent)
    {
    }

    public void windowClosed(WindowEvent e)
    {
        ecriture.println("END");
    }

    public void windowClosing(WindowEvent e)
    {
        String mes = "END";
        ecriture.println(mes);
        ecriture.close();
    }

    public void windowDeactivated(WindowEvent windowevent)
    {
    }

    public void windowDeiconified(WindowEvent windowevent)
    {
    }

    public void windowIconified(WindowEvent windowevent)
    {
    }

    public void windowOpened(WindowEvent windowevent)
    {
    }

    public void keyPressed(KeyEvent e)
    {
        if(e.getKeyCode() == 10)
        {
            String mess = text.getText();
            if(mess.compareToIgnoreCase("") != 0)
            {
                String coul = (new StringBuilder(String.valueOf(OXconverter.convOX(couleur.getRed())))).append(OXconverter.convOX(couleur.getGreen())).append(OXconverter.convOX(couleur.getBlue())).toString();
                mess = (new StringBuilder("<html><font color=#")).append(coul).append(">").append(login).append(":").append(mess).append("</font></html>").toString();
                ecriture.println(mess);
                text.setText("");
            }
        } else
        if(e.isAltDown() && e.isControlDown() && (e.getKeyCode() == 67 || e.getKeyCode() == 99))
        {
            couleur = JColorChooser.showDialog(this, "choix de la couleur du texte \340 envoyer?", Color.red);
            text.setForeground(couleur);
        }
    }

    public void keyReleased(KeyEvent keyevent)
    {
    }

    public void keyTyped(KeyEvent keyevent)
    {
    }

    JList message;
    static Color couleur;
    JTextField text;
    Socket socket;
    int port;
    BufferedReader lecture;
    PrintWriter ecriture;
    String Ipserveur;
    String login;
    String Etat;

    static 
    {
        couleur = Color.PINK;
    }
}
